﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReminderManager.cs" company="Derek Lakin">
//   2011 Derek Lakin
// </copyright>
// <summary>
//   Provides a static helper class for managing reminders.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Services.Scheduling
{
    using System;
    using Microsoft.Phone.Scheduler;

    /// <summary>
    /// Provides a static helper class for managing reminders.
    /// </summary>
    /// <remarks>See http://wp7trenches.wordpress.com/2011/08/10/dont-forget/ for details.</remarks>
    public static class ReminderManager
    {
        /// <summary>
        /// Adds a new reminder.
        /// </summary>
        /// <param name="name">The name of the reminder.</param>
        /// <param name="title">The title of the notification.</param>
        /// <param name="body">The body of the notification.</param>
        /// <param name="beginTime">The time of the reminder.</param>
        /// <param name="navigationUri">The URI to be passed to the application on launch.</param>
        /// <param name="recurrence">The recurrence of the reminder.</param>
        /// <returns>The new <see cref="Reminder"/> instance.</returns>
        public static Reminder AddReminder(
            string name,
            string title,
            string body,
            DateTime beginTime,
            Uri navigationUri,
            RecurrenceInterval recurrence = RecurrenceInterval.None)
        {
            if (HasReminder(name))
            {
                RemoveReminder(name);
            }

            var reminder = new Reminder(name)
            {
                BeginTime = beginTime,
                Content = body,
                NavigationUri = navigationUri,
                RecurrenceType = recurrence,
                Title = title
            };
            ScheduledActionService.Add(reminder);

            return reminder;
        }

        /// <summary>
        /// Gets the reminder with the specified name.
        /// </summary>
        /// <param name="name">The name of the reminder.</param>
        /// <returns>A <see cref="Reminder"/> instance if found; otherwise <c>null</c>.</returns>
        public static Reminder GetReminder(string name)
        {
            var reminder = ScheduledActionService.Find(name) as Reminder;
            return reminder;
        }

        /// <summary>
        /// Determines whether there is a reminder with the specified name.
        /// </summary>
        /// <param name="name">The name of the reminder.</param>
        /// <returns>
        ///<c>True</c> if there is a reminder with the specified name; otherwise, <c>false</c>.
        /// </returns>
        public static bool HasReminder(string name)
        {
            var reminder = GetReminder(name);
            return reminder != null;
        }

        /// <summary>
        /// Removes the reminder with the specified name.
        /// </summary>
        /// <param name="name">The name of the reminder.</param>
        public static void RemoveReminder(string name)
        {
            if (HasReminder(name))
            {
                ScheduledActionService.Remove(name);
            }
        }
    }
}
